#!/bin/sh
#
# Start/stop the Bluetooth daemons
#

DAEMON_NAME="hcid"
HID2HCI_NAME="hid2hci"
HIDD_NAME="hidd"
RFCOMM_NAME="rfcomm"
PAND_NAME="pand"
DUND_NAME="dund"

DAEMON_EXEC="/usr/sbin/hcid"
HID2HCI_EXEC="/usr/sbin/hid2hci"
HIDD_EXEC="/usr/bin/hidd"
RFCOMM_EXEC="/usr/bin/rfcomm"
PAND_EXEC="/usr/bin/pand"
DUND_EXEC="/usr/bin/dund"

DAEMON_ENABLE="true"
HID2HCI_ENABLE="false"
HIDD_ENABLE="false"
RFCOMM_ENABLE="false"
DUND_ENABLE="false"
PAND_ENABLE="false"

RFCOMM_CONFIG="/etc/bluetooth/rfcomm.conf"

HIDD_OPTIONS=""
DUND_OPTIONS=""
PAND_OPTIONS=""

case "$1" in
  start)
    if [ "$DAEMON_ENABLE" = "true" -a -x "$DAEMON_EXEC" ] ; then
      $DAEMON_EXEC
    fi
    if [ "$HID2HCI_ENABLE" = "true" -a -x "$HID2HCI_EXEC" ] ; then
      $HID2HCI_EXEC --tohci > /dev/null 2>&1 || true
    fi
    if [ "$SDPD_ENABLE" = "true" -a -x "$SDPD_EXEC" ] ; then
      $SDPD_EXEC
    fi
    if [ "$HIDD_ENABLE" = "true" -a -x "$HIDD_EXEC" ]; then
      $HIDD_EXEC $HIDD_OPTIONS --server
    fi
    if [ "$RFCOMM_ENABLE" = "true" -a -x "$RFCOMM_EXEC" -a -f "$RFCOMM_CONFIG" ]; then
      $RFCOMM_EXEC -f $RFCOMM_CONFIG bind all
    fi
    if [ "$DUND_ENABLE" = "true" -a -x "$DUND_EXEC" -a -n "$DUND_OPTIONS" ]; then
      $DUND_EXEC $DUND_OPTIONS
    fi
    if [ "$PAND_ENABLE" = "true" -a -x "$PAND_EXEC" -a -n "$PAND_OPTIONS" ]; then
      $PAND_EXEC $PAND_OPTIONS
    fi
    ;;
  stop)
    killall $PAND_NAME >/dev/null 2>&1
    killall $DUND_NAME >/dev/null 2>&1
    
    if [ -x "$RFCOMM_EXEC" ]; then
      $RFCOMM_EXEC release all >/dev/null 2>&1
    fi

    killall $HIDD_NAME >/dev/null 2>&1
    killall $SDPD_NAME >/dev/null 2>&1
    killall $DAEMON_NAME >/dev/null 2>&1
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 [start|stop|restart]"  
esac
